package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ruiysoft.crm.dto.RoleEx;
import com.ruiysoft.crm.dto.SysPowerEx;
import com.ruiysoft.crm.entity.Course;
import com.ruiysoft.crm.entity.SysPower;
import com.ruiysoft.crm.entity.SysRole;
import com.ruiysoft.crm.entity.SysRolePower;
import com.ruiysoft.crm.mapper.SysPowerMapper;
import com.ruiysoft.crm.mapper.SysRoleMapper;
import com.ruiysoft.crm.mapper.SysRolePowerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2018-08-11
 */
@RestController
@RequestMapping("role")
public class SysRoleController {
    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysRolePowerMapper sysRolePowerMapper;

    @PostMapping("create")
    public Object create(@RequestBody RoleEx roleEx) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (sysRoleMapper.insert(roleEx) > 0) {
                for (String id: roleEx.getIds()
                     ) {
                    SysRolePower sysRolePower = new SysRolePower();
                    sysRolePower.setRoleId(roleEx.getId());
                    sysRolePower.setPowerId(Integer.parseInt(id));

                    if(sysRolePowerMapper.insert(sysRolePower) > 0)
                        map.put("success", true);
                }
            }

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("queryByEnterpriseId")
    @GetMapping("queryByEnterpriseId")
    public Object queryByEnterpriseId(String enterpriseId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            List<SysRole> list = sysRoleMapper.selectList(new EntityWrapper<SysRole>().eq("enterpriseId", enterpriseId));
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("query")
    @GetMapping("query")
    public Object query() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            EntityWrapper<SysRole> entityWrapper = new EntityWrapper();
            List<SysRole> list = sysRoleMapper.selectPage(new Page<SysRole>(1, 25), entityWrapper);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @GetMapping("delete")
    public Object delete(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (sysRoleMapper.deleteById(id) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @Autowired
    SysPowerMapper sysPowerMapper;

    @Cacheable("isHaveSubItem")
    boolean isHaveSubItem(Integer parentId) {
        List<SysPower> list = sysPowerMapper.selectList(new EntityWrapper<SysPower>().eq("parentCode", parentId));
        if(list.size() > 0)
            return true;
        else
            return false;
    }

    @Cacheable("getAsyncPowerTree")
    @GetMapping("getAsyncPowerTree")
    public Object getAsyncPowerTree(Integer treeNodeId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            List<SysPower> list = sysPowerMapper.selectList(new EntityWrapper<SysPower>().eq("parentCode", treeNodeId));
            List<SysPowerEx> list2 = new ArrayList();
            for (SysPower sysPower: list
                 ) {
                SysPowerEx sysPowerEx = new SysPowerEx();
                sysPowerEx.setKey(sysPower.getId() + "");
                sysPowerEx.setTitle(sysPower.getName());
                if(!isHaveSubItem(sysPower.getId())) {
                    sysPowerEx.setLeaf(true);
                    sysPowerEx.setChecked(true);
                }

                list2.add(sysPowerEx);
            }

            map.put("success", true);
            map.put("data", list2);


        } catch (Exception error) {
            error.printStackTrace();
        }

        return map;
    }

}
